<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: category.php Sat Jul 18 13:17:11 CST 2009
 */

if ( !defined("XOOPS_ROOT_PATH") )  exit;
if ( !class_exists("xoGroups") ) {
	include dirname(dirname(__FILE__))."/include/groups.php";
}
class groupsStyle extends XoopsObject 
{
	public function __construct() {
}

class groupsStyleHandler extends XoopsPersistableObjectHandler
{
	public function __construct($db) {
        parent::__construct($db,"groups_style","groupsStyle","style_id");
    }
    /**
     * for groups category create and modify
     * @param array $params
     * @return int $cat_id
     */
     public function setStyle(array $params) {
        if ( empty($params) || !is_array($params) ) return false;
         
        $myts =  MyTextSanitizer::getInstance();
    	$group_id = isset($params["group_id"]) ? intval($params["group_id"]) : 0 ;
        
        if ( !empty($cat_id) ) {
            $obj = $this->get($cat_id);
        } else {
            $obj = $this->get();
            $obj->setVar("cat_create_time",time());
        }
        
    	foreach ( $params as $key => $val ) {
			if ( !isset($obj->vars[$key]) || $myts->stripSlashesGPC($val) == $obj->getVar($key) ) continue;
			$obj->setVar($key, $val);
		}
		
    	if ( $cat_id = $this->insert($obj) ) {
    	    return $cat_id;
    	}
    	return false;
    }
    
    public function getCatSelect() {
        $params = array(
            "fields"=> array("cat_name","cat_group_total"),
            "order"=>"DESC",
            "sort"=>"cat_order"
        );
        $objs = $this->getCategories($params);
        $ret = array();
        if ( !empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $ret[$k] = $obj->getVar("cat_name","n") . "(" . $obj->getVar("cat_group_total") . ")";
            }
        }
        return $ret;
    }
    /**
     * for groups category create and modify
     * @param array $params
     * @param bool  $asObj
     * @return array of objects/array 
     */
    public function getCategories( array $params=array(), $asObj = true ) {
        $criteria = xoGroups::getCriteria($params);
        $fields = ( isset($params["fields"]) && is_array($params["fields"]) ) ? $params["fields"] : null ;
        $ret = $this->getAll($criteria, $fields, $asObj);
        return $ret;
    }
    
	/**
     * for footprint page nav
     *
     * @param array $params
     * @return string $pagenav
     */
    public function getPageNav( array $params ) {
        $pagenav = "";
        $criteria = xoGroups::getCriteria($params);
        $counts = $this->getCount($criteria);		
        $extra = isset($params["extra"]) ? trim($params["extra"]) : "";
        if( $counts > $params["limit"]){	
        	$nav = new XoopsPageNav($counts, $params["limit"], $params["start"], "start",$extra);
        	$pagenav = $nav->renderNav(4);
        }
        return $pagenav;
    }
	
    /**
     * update group category groups field
     *
     * @param int $cat_id
     * @param int $val
     * @return bool value
     */
    public function updateGroupTotal($cat_id,$val=1) {
        $obj = $this->get($cat_id);
        if ( empty($obj) || !is_object($obj) || $obj->isNew() ) return false;
        $obj->setVar("cat_group_total",(int)$obj->getVar("cat_group_total") + intval($val) );
        if ( $this->insert($obj) ) {
            return true;
        }
        return false;
    }
    
    /**
     * update group category cache file
     * @return bool
     */
    public function updateCatCache() {
        return true;
    }
    
    /**
     * delete a category
     *
     * @param int $cat_id
     * @return boot 
     */
    public function deleleCat($cat_id) {
        return true;
    }
}
